/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.eng.scc.core.view;

import com.eng.scc.core.panes.ApplicationPane;
import com.eng.scc.core.panes.LoginPane;
import com.eng.scc.core.parts.AbstractApplicationAction;
import com.eng.scc.core.parts.ActionCategory;
import com.eng.scc.core.parts.SerialReceiverPane;
import java.awt.BorderLayout;
import java.awt.CardLayout;
import java.awt.Dimension;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

/**
 *
 * @author Rafael
 */
public class LauncherApplicationView extends JFrame
{
    private JPanel cards;

    public LauncherApplicationView()
    {
        try
        {
            UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
            initComponents();
        }
        
        catch ( Exception ex )
        {
            Logger.getLogger( LauncherApplicationView.class.getName() ).log( Level.SEVERE, null, ex );
        }
        
        setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
        setSize( 800, 600 );
        setTitle( "Gereciador de Transportadora" );
    }

    private void loginSucess()
    {
          CardLayout cl = (CardLayout)(cards.getLayout());
          ApplicationContext.getInstance().setActiveUser( null );
          cl.show(cards, APPS );
    }
    
    private void buildMenuBar()
    {
        jMenuBar = new JMenuBar();
        
        if ( applicationPane.getViewActions() != null )
        {
            for ( ActionCategory actionCategory : applicationPane.getViewActions() )
            {
                JMenu jMenu = new JMenu( actionCategory.getName() );
                
                for ( AbstractApplicationAction abstractApplicationAction : actionCategory.getActions() )
                {
                    JMenuItem item = new JMenuItem( abstractApplicationAction );
                    item.setToolTipText( abstractApplicationAction.getToolTip() );
                    jMenu.add( item );
                }
                
                jMenuBar.add( jMenu );
            }
        }
        
        this.setJMenuBar( jMenuBar );
    }
    
    private void initComponents()
    {
        setLayout( new BorderLayout() );
        
        loginPane.addEventListener( new ChangeListener() 
        {
            @Override
            public void stateChanged( ChangeEvent e )
            {
                loginSucess();
            }

        } );
        
        cards = new JPanel(new CardLayout());
        cards.add( applicationPane, APPS );
        cards.add( loginPane, LOGIN );
        
        add( cards, BorderLayout.CENTER );
        add( serialReceiverPane, BorderLayout.SOUTH );
        
        applicationPane.addEventListener( new ChangeListener() {

            @Override
            public void stateChanged( ChangeEvent e )
            {
                buildMenuBar();
            }
        });
        
        this.setJMenuBar( jMenuBar );
        /*REMOVE THIS SCOPE*/
        ApplicationContext.getInstance().setActiveUser( null );
    }
    
    private static final String LOGIN = "login";
    private static final String APPS = "apps";
    
    private LoginPane loginPane = new LoginPane();
    private ApplicationPane applicationPane = new ApplicationPane();
    private JMenuBar jMenuBar = new JMenuBar();
    private SerialReceiverPane serialReceiverPane = new SerialReceiverPane();
    
    public static void main( String... args )
    {
        SwingUtilities.invokeLater( new Runnable() 
        {
            @Override
            public void run()
            {
                LauncherApplicationView view = new LauncherApplicationView();
                view.setVisible( true );
            }
        });
    }
}
